import router from './router'
import { routes } from '@/router'
import store from './store'
import { getToken } from '@/utils/auth' // 从cookie中获取用户信息

// import * as MenuAPI from '@/api/menu'
// import defaultLayout from '@/views/layout/default/index'
// import Submenu from '@/views/layout/submenu/index'

// const _import = require('./router/_import_' + process.env.NODE_ENV)
var getRouter // 用来获取后台拿到的路由

router.beforeEach((to, from, next) => {
  console.info(from)
  console.info(to)
  if (getToken()) { // 判断是否已存在token信息
    if (to.path.indexOf('/login') === 0) {
      next({ path: '/' })
    } else {
      if (store.getters.user === null) { // 判断当前用户是否已拉取完用户信息
        console.info('store.getters.user === null')
        store.dispatch('GetUserInfo').then(data => { // 拉取用户信息
          if (!getRouter) { // 不加这个判断，路由会陷入死循环
            getRouter = routes
            console.info(getRouter)
            routerGo(to, next)// 执行路由跳转方法
          } else {
            next()
          }
          console.info('')
          next()
        }).catch((err) => {
          store.dispatch('FedLogOut').then(() => {
            console.info(err)
            // Message.error(err || '登录失败')
            next({ path: '/' })
          })
        })
      } else {
        console.info(global.antRouter)
        next()
      }
    }
  } else {
    if (to.path.indexOf('/login') === 0) {
      next()
    } else if (window.location.href.indexOf('/#/ssologin') >= 0) {
      next()
    } else if (window.location.href.indexOf('/ssologin') >= 0) {
      window.location.href = '/#/ssologin' + window.location.search
    } else {
      next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
    }
  }
})

router.afterEach(() => {
})

function routerGo (to, next) {
  getRouter = filterAsyncRouter(getRouter) // 过滤路由
  global.antRouter = getRouter // 将路由数据传递给全局变量，做侧边栏菜单渲染工作
  console.info(global.antRouter)
  console.info(to)
  next({ ...to, replace: true })
}

function filterAsyncRouter (asyncRouterMapForPromission) { // 遍历后台传来的路由字符串，转换为组件对象
  const accessedRouters = asyncRouterMapForPromission.filter(route => {
    if (route.path) {
      if (route.path !== '/') {
        route.to = route.path
      } else {
        route.to = null
      }
    } else {
      route.to = '/'
    }
    if (route.name) {
      route.title = route.name
    } else {
      route.title = 'login'
    }

    delete route.path
    delete route.name
    delete route.component

    // if (route.component) {
    //   if (route.component === 'Layout') { // Layout组件特殊处理
    //     // route.component = defaultLayout
    //   } else if (route.component === 'Submenu') {
    //     // route.component = Submenu
    //   } else {
    //     try {
    //       route.component = _import(route.component)
    //     } catch (e) {
    //       // route.component = require('@/components/ErrorPage/404.vue').default
    //       console.log(e)
    //     }
    //   }
    // }
    if (route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })

  return accessedRouters
}
